package com.yuncheng.oaApi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.entity.NkRsBxlc;
import com.yuncheng.oaApi.entity.NkRsnd;
import com.yuncheng.oaApi.service.INkRsBxlcService;
import com.yuncheng.oaApi.service.INkRsNdService;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 内控-年度
 * @Author: lhb
 * @Date: 2023-11-21
 * @Version: V1.0
 */
@Api(tags = "内控-年度接口")
@RestController
@RequestMapping("/api/spzx-oa/nkRsNd")
public class NkRsNdController  {

    private static final Logger log = LoggerFactory.getLogger(NkRsNdController.class);

    @Resource
    private INkRsNdService nkRsNdService;




    @AutoLog(value = "内控-年度-添加")
    @ApiOperation(value = "内控-年度-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody NkRsnd nkRsnd) {

        List<NkRsnd> nkRsnds = nkRsNdService.list(new LambdaQueryWrapper<NkRsnd>());
        for (NkRsnd nkRsnd1 : nkRsnds){
            if (nkRsnd.getFnd().equals(nkRsnd1.getFnd())){
                throw new RuntimeException("当前年度已添加");
            }
        }
        nkRsnd.setFsfxz(OaCommonFlowConstant.NDXZ_NO);
        nkRsNdService.save(nkRsnd);
        return HttpResult.ok("添加成功");
    }


    @AutoLog(value = "内控-年度-选择")
    @ApiOperation(value = "内控-年度-选择")
    @GetMapping(value = "/edit")
    public HttpResult<?> edit(String fnd) {
        List<NkRsnd>  nkRsnds = new ArrayList<>();
         nkRsnds = nkRsNdService.list(new LambdaQueryWrapper<NkRsnd>()
                .eq(NkRsnd::getFnd, fnd));
        if (CollectionUtils.isEmpty(nkRsnds)){
            NkRsnd nkRsnd = new NkRsnd();
            nkRsnd.setFnd(fnd);
            nkRsNdService.save(nkRsnd);
            nkRsnds = nkRsNdService.list(new LambdaQueryWrapper<NkRsnd>()
                    .eq(NkRsnd::getFnd, fnd));

        }
        NkRsnd nkrsnd = nkRsNdService.getById(nkRsnds.get(0).getId());
        nkrsnd.setFsfxz(OaCommonFlowConstant.NDXZ_YES);
        //然后将其他的所有的年度设置为否
        List<NkRsnd> nkrsndlist = nkRsNdService.list(new LambdaQueryWrapper<NkRsnd>()
                .ne(NkRsnd::getId, nkrsnd.getId()));
        for (NkRsnd nkRsnd1 : nkrsndlist){
            nkRsnd1.setFsfxz(OaCommonFlowConstant.NDXZ_NO);
            nkRsNdService.updateById(nkRsnd1);
        }
        nkRsNdService.updateById(nkrsnd);
        return HttpResult.ok("修改成功");
    }


    @AutoLog(value = "内控-年度-列表")
    @ApiOperation(value = "内控-年度-列表")
    @PutMapping(value = "/ndList")
    public HttpResult<?> ndList() {

        List<NkRsnd> nkRsnds = nkRsNdService.list(new LambdaQueryWrapper<NkRsnd>());
        return HttpResult.ok(nkRsnds);
    }


    @AutoLog(value = "内控-获取当前选择的年度")
    @ApiOperation(value = "内控-获取当前选择的年度")
    @GetMapping(value = "/hqndList")
    public HttpResult<?> hqndList() {

        List<NkRsnd> nkRsnds = nkRsNdService.list(new LambdaQueryWrapper<NkRsnd>()
                .eq(NkRsnd::getFsfxz, OaCommonFlowConstant.NDXZ_YES));
        String fnd = nkRsnds.get(0).getFnd();
        HttpResult<Object> HttpResult = new HttpResult<>();

        HttpResult.setResult(fnd);
        return HttpResult;
    }




}
